package com.zxl.app.controller.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.zxl.app.service.WxMaUserService;
import com.zxl.common.Result;
import com.zxl.common.constant.ResponseStatusCode;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.model.bo.wx.ActiveAccountBo;
import com.zxl.common.model.bo.wx.WxUserLoginBo;
import com.zxl.common.model.pojo.SysMember;
import com.zxl.common.model.vo.WxUserLoginVo;
import com.zxl.common.service.SysMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.UUID;

/**
 * @author zxw
 * @date 2023/3/31
 */
@RestController
@RequestMapping("/wx/user")
@Slf4j
@Api(tags = {"微信接口"})
public class WxMaUserController {

    @Autowired
    private WxMaService wxMaService;
    @Resource
    private WxMaUserService wxMaUserService;
    @Resource
    private SysMemberService sysMemberService;

    /**
     * 登陆接口
     */
    @ApiOperation("登陆")
    @PostMapping("/login")
    public Result<WxUserLoginVo> login(@RequestBody WxUserLoginBo wxUserLoginBo) {
        WxUserLoginVo wxUserLoginVo = new WxUserLoginVo();
        if (StringUtils.isBlank(wxUserLoginBo.getCode())) {
            throw new BusinessException("小程序登陆异常");
        }
        try {

            WxMaJscode2SessionResult session = wxMaService.jsCode2SessionInfo(wxUserLoginBo.getCode());
            String unionId = session.getUnionid();
            String openId = session.getOpenid();
            String sessionKey = session.getSessionKey();
            SysMember member = new SysMember();
            member.setOpenId(openId);
            member.setUnionId(unionId);
            member.setCreateTime(new Date());
            member.setPhone(wxUserLoginBo.getPhone());
            String token = UUID.randomUUID().toString();
            SysMember record = sysMemberService.chooseHasOpenIdMember(unionId, openId);
            /*  第一次登录  */
            if (record == null) {
                wxMaUserService.firstLogin(sessionKey, token, member);
                wxUserLoginVo.setOpenId(openId);
                wxUserLoginVo.setToken(token);
                wxUserLoginVo.setPhone(wxUserLoginBo.getPhone());
            } else {
                wxUserLoginVo = wxMaUserService.updateKeyInfo(record, token, sessionKey, member);
            }

            if (wxUserLoginVo.getPhone() == null){
                return Result.fail(ResponseStatusCode.NOT_PHONE.getCode(),ResponseStatusCode.NOT_PHONE.getMessage());
            }


        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return new Result(wxUserLoginVo);
    }


    @ApiOperation("通过加密数据串获取手机号激活帐户")
    @PostMapping("/active_account")
    public Result<WxUserLoginVo> activeAccount(@Valid @RequestBody ActiveAccountBo bo) {
        return new Result(wxMaUserService.activeAccount(bo));
    }


}